package ls.vuecp.gui;

import java.awt.Component;
import java.awt.Dimension;

import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;

import tufts.vue.DEBUG;

import ls.vuecp.gui.component.LWMap;
import ls.vuecp.gui.view.MapViewer;

public class MapTabbedPane extends JTabbedPane
{
	private static MapTabbedPane lastFocusPane;
    private static int lastFocusIndex = -1;
    
	public MapTabbedPane(String name, boolean isLeft)
	{
		this.name = name;
		setName("mapTabs-"+name);
		
		setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
		setFocusable(false);
		setTabPlacement(javax.swing.SwingConstants.TOP);
		setPreferredSize(new Dimension(300,400));
	}
	
	private final String name;
	
	public MapViewer getViewerAt(int index) {
        Object c;
        try {
            c = getComponentAt(index);
        } catch (ArrayIndexOutOfBoundsException e) {
            return null;
        }
        
        MapViewer viewer = null;
        if (c instanceof MapViewer)
            viewer = (MapViewer) c;
        else if (c instanceof JScrollPane)
            viewer = (MapViewer) ((JScrollPane)c).getViewport().getView();
        return viewer;
    }
	public LWMap getMapAt(int index) {
        MapViewer viewer = getViewerAt(index);
        LWMap map = null;
//         if (viewer == null && VUE.inFullScreen()) // hack, but works for now: todo: cleaner
//             return VUE.getActiveMap();
        if (viewer != null)
            map = viewer.getMap();
        //System.out.println(this + " map at index " + index + " is " + map);
        return map;
    }
	public void addViewer(MapViewer viewer) {
        
        Component c = new ls.vuecp.gui.component.MapScrollPane(viewer);

//        if (false) {
//            String tabTitle = viewerToTabTitle(viewer);
//            if (tabTitle == null)
//                tabTitle = "unknown";
//            
//            addTab(tabTitle, c);
//        } else {
            addTab(viewerToTabTitle(viewer), c);
//        }
        
        LWMap map = viewer.getMap();
//        map.addLWCListener(this, LWKey.MapFilter, LWKey.Label);
        // todo perf: we should be able to ask to listen only
        // for events from this object directly (that we don't
        // care to hear from it's children)
//        updateTitleTextAt(indexOfComponent(c)); // first time just needed for tooltip
    }
//	private String mapToTabTitle(LWMap map) {
//        String title = map.getLabel();
//        if (title.toLowerCase().endsWith(".vue") && title.length() > 4)
//            title = title.substring(0, title.length() - 4);
//        if (map.isCurrentlyFiltered())
//            title += "*";
//        return title;
//    }

    private String viewerToTabTitle(MapViewer viewer) {

//    	return "unknown";

            return mapToTabTitle(viewer.getMap());// + " (" + 
            			//ZoomTool.prettyZoomPercent(viewer.getZoomFactor()) + ")";

    }
    private String mapToTabTitle(LWMap map) {
        String title = map.getLabel();
        if (title.toLowerCase().endsWith(".vue") && title.length() > 4)
            title = title.substring(0, title.length() - 4);
//        if (map.isCurrentlyFiltered())
//            title += "*";
        return title;
    }
}
